import {createApp} from 'vue'
import {createPinia} from 'pinia'

import ElementPlus from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

import 'core-js/actual'

import 'element-plus/dist/index.css'

import CKEditor from '@ckeditor/ckeditor5-vue';


import App from './App.vue'
import router from './router'
import moment from 'moment'




import './assets/main.css'

const app = createApp(App)

app.use(ElementPlus)

import mitt from 'mitt'
const bus = mitt()
app.config.globalProperties.$bus = bus


// @ts-ignore
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

app.directive('role', {
    mounted(el, binding) {
        const userRole = localStorage.getItem("rule")
        const allowedRoles = binding.value // 获取允许显示该组件的角色列表
        if (!allowedRoles.includes(userRole)) {
            el.style.display = 'none' // 不允许显示该组件，隐藏
        }
    }
})


app.config.globalProperties.$filters = {
    formatDateDiff(start: string, end: string) {
        if (start === '' || end === '') {
            return ''
        }
        var seconds = moment(end).diff(moment(start), 'seconds')
        var h = Math.floor(seconds / 3600);
        var m = Math.floor((seconds / 60 % 60));
        var s = Math.floor((seconds % 60));
        return `${h}时${m}分${s}秒`
    },
    formatDateDate(value: string) {
        if (value === '' || value === null) {
            return ''
        }
        return moment(value).format('YYYY-MM-DD')
    },
    formatDate(value: string) {
        if (value === '' || value === null) {
            return ''
        }
        return moment(value).format('YYYY-MM-DD HH:mm:ss')
    },
    sizeFilter(bytesize: number) {
        let $i = 0
        // 当$bytesize 大于是1024字节时，开始循环，当循环到第4次时跳出；
        while (Math.abs(bytesize) >= 1024) {
            bytesize = bytesize / 1024
            $i++
            if ($i === 4) break
        }
        // 将Bytes,KB,MB,GB,TB定义成一维数组；
        const $units = ['Bytes', 'KB', 'MB', 'GB', 'TB']
        // @ts-ignore
        const $newsize = Math.round(bytesize, 2)
        return $newsize + ' ' + $units[$i]
    }

}

app.use(createPinia())
app.use(router)
app.use(CKEditor)

app.mount('#app')
